package com.wanmait.recruit.controller;

import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.pojo.Industry;
import com.wanmait.recruit.pojo.PositionType;
import com.wanmait.recruit.service.IndustryService;
import com.wanmait.recruit.service.PositionTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Transactional
@RequestMapping("/manage/positionType")
public class PositionTypeController {
    @Resource
    private PositionTypeService positionTypeService;
    @Resource
    private IndustryService industryService;
    @RequestMapping("positionTypeList")
    public ModelAndView positionTypeList(){
        ModelAndView modelAndView = new ModelAndView();
        List<Industry> industries = industryService.findAll();
        modelAndView.addObject("industries",industries);
        modelAndView.setViewName("/manage/positionType/positionTypeList");
        return modelAndView;
    }
    @RequestMapping("positionTypeListData")
    public ModelAndView positionTypeListData(String name,Integer pageNum,Integer typeId){
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<PositionType> pageInfo = positionTypeService.findAll(name, pageNum,typeId);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("/manage/positionType/positionTypeListData");
        return modelAndView;
    }
    @RequestMapping("delete")
    public String delete(Integer id){
        positionTypeService.deleteById(id);
        return "redirect:/manage/positionType/positionTypeList";
    }
    @RequestMapping("insert")
    public ModelAndView insert(){
        ModelAndView modelAndView = new ModelAndView();
        List<Industry> industries = industryService.findAll();
        modelAndView.addObject("industries",industries);
        modelAndView.setViewName("/manage/positionType/insert");
        return modelAndView;
    }
    @PostMapping("insert")
    public String insert(String name,Integer heat,Integer typeId){
        PositionType positionType = new PositionType();
        positionType.setPositionTypeName(name);
        positionType.setHeat(heat);
        positionType.setIndustryId(typeId);
        positionTypeService.insert(positionType);
        return "redirect:/manage/positionType/insert";
    }
    @RequestMapping("isRename")
    @ResponseBody
    public String isRename(String name){
        Boolean flag = positionTypeService.isRename(name);
        if(flag){
            return "1";
        }else{
            return "0";
        }
    }
}
